/**
 * 根据传入的表名和时间范围，查询数据库中交易时间符合时间范围的数据
 * @param 表名，前端传参body（里面包含交易时间的时间范围）
 * @returns Promise数据库数据
 */
import db from "../db/db.js";
import timestampToDateString from "./timestampToDateString.js";

const getTimeToData = async (tableName: string, params:any) => {
    let startTimeTrans;
    let endTimeTrans;
    if (params.timestampRange && params.timestampRange.length) {
        startTimeTrans = timestampToDateString(params.timestampRange[0]);
        endTimeTrans = timestampToDateString(params.timestampRange[1]);        
    }
    let sql = `SELECT * FROM ${tableName} WHERE tradingTime BETWEEN ? AND ?`;
    let sqlParams = [startTimeTrans, endTimeTrans];
    if (params.ignore) {
        sql += ` AND ignoreFlag = ?`;
        sqlParams.push(0);
    }
    return new Promise((resolve, reject) => {
        db.query(sql, sqlParams, (err, results) => {
            if (err) {
                return reject(err);
            }
            resolve(results);
        });
    });
};

export default getTimeToData;
